package weaver.interfaces.suntai.utils;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.TimeUtil;
import weaver.general.Util;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;


/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-05-12
 * @Description: 工具类
 * @ModifyLog:
 **/
public class SunTaiUtil {

    /*
     *@Description:建模权限重构
     */
    public static void getNewModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight( true );
        modeRightInfo.editModeDataShare( userid, formmodeid, id );
    }

    /*
     *@Description:接口调用日志表
     */
    public static void saveCallInterLog(String jkmc,String fhjg,String lcid, int userid) {
        String rqsj = DateUtil.getFullDate();
        int formmodeid = getFormmodeId( "uf_jkrzb" );
        Map<String, String> dataMap = new HashMap();
        dataMap.put( "jkmc", jkmc);
        dataMap.put( "jklx", "0" );
        dataMap.put( "rqsj", rqsj );
        dataMap.put( "fhjg", fhjg);
        dataMap.put( "lcid", lcid);
        insertModelData( formmodeid, userid, dataMap );
    }

    /*
    *@Description: 将数据插入表单建模
    */
    public static int insertModelData(int modeid, int userid, Map map) {
        int dataid = 0;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        //查询该模块表名
        String sql = "select b.tablename,a.formid from modeinfo a left join workflow_bill b on a.formid=b.id where a.id=?";
        rs.executeQuery( sql, modeid );
        rs.next();
        String tablename = Util.null2String( rs.getString( "tablename" ) );
        String uuid = UUID.randomUUID().toString();
        String insSql = "insert into " + tablename
                + "(uuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values(?,?,?,?,?)";
        Object[] params = {uuid, userid, TimeUtil.getCurrentDateString(), TimeUtil.getOnlyCurrentTimeString(), modeid};
        if (rs.executeUpdate( insSql, params )) {
            rs1.executeQuery( "select id from " + tablename + " where uuid=?", uuid );
            rs1.next();
            dataid = Util.getIntValue( rs1.getString( "id" ) );
            if (dataid > 0) {
                //遍历数据 进行update
                String updatesql = "update " + tablename + " set ";
                Set<String> keySet = map.keySet();
                for (String key : keySet) {
                    updatesql += key + "='" + map.get( key ).toString() + "',";
                }
                if (updatesql.endsWith( "," )) {
                    updatesql = updatesql.substring( 0, updatesql.length() - 1 );
                    updatesql += " where id=" + dataid;
                    rs.executeUpdate( updatesql );
                }
                //进行权限重构
                getNewModeRight( userid, modeid, dataid );
            }
        }
        return dataid;
    }

    /*
     *@Description: 获取formmodeid
     */
    public static int getFormmodeId(String tableName) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select m.id from modeinfo m left join WORKFLOW_BILL b on m.formid = b.id where b.tablename = ?", tableName );
        rs.next();
        int formmodeid = Util.getIntValue( rs.getString( "id" ) );
        return formmodeid;
    }

    /*
     *@Description: 获取表名
     */
    public static String getTableName(String modeid ){
        RecordSet rs = new RecordSet(  );
        String sql = "select b.tablename,a.formid from modeinfo a left join workflow_bill b on a.formid=b.id where a.id=?";
        rs.executeQuery(sql,modeid);
        rs.next();
        String tablename= Util.null2String(rs.getString("tablename"));
        return tablename;
    }

    /*
     *@Description: 根据表单和字段获取下拉框值
     */
    public static String getSelectItem(int billid, String fieldname, String selectvalue) {
        RecordSet rs = new RecordSet();
        String sql = "select t1.selectvalue,t1.selectname from workflow_selectitem t1 left join workflow_billfield t2\n" +
                "on t1.fieldid=t2.id where abs(t2.billid)=? and t2.fieldname=? and t1.selectvalue=?";
        rs.executeQuery( sql, billid, fieldname, selectvalue );
        rs.next();
        String selectname = Util.null2String( rs.getString( "selectname" ) );
        return selectname;
    }


}

